﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using PDAMain.DAL.Dao;
using System.Data.SQLite;

namespace PDAMain.DAL
{
    class DataHelper
    {
        private static string DB_FILE_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + @"\local.db3";

        /// <summary>
        /// 定义数据同步操作对象
        /// </summary>
        private static SynchronizationDataDao sdDao = null;
        public static SynchronizationDataDao SynchronizationDataDao
        {
            get
            {
                if (sdDao == null)
                {
                    sdDao = new SynchronizationDataDao();
                    sdDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return sdDao;
            }
        }

        /// <summary>
        /// 定义客服工单数据操作对象
        /// </summary>
        private static CustomerWorkDao cwDao = null;
        public static CustomerWorkDao CustomerWorkDao
        {
            get
            {
                if (cwDao == null)
                {
                    cwDao = new CustomerWorkDao();
                    cwDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return cwDao;
            }
        }
        
        /// <summary>
        /// 定义巡检工单数据操作对象
        /// </summary>
        private static InspectionWorkDao iwDao = null;
        public static InspectionWorkDao InspectionWorkDao
        {
            get
            {
                if (iwDao == null)
                {
                    iwDao = new InspectionWorkDao();
                    iwDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return iwDao;
            }
        }
        /// <summary>
        /// 定义抄电表工作单数据操作对象
        /// </summary>
        private static ReadingAmmeterDao ramDao = null;
        public static ReadingAmmeterDao ReadingAmmeterDao
        {
            get
            {
                if (ramDao == null)
                {
                    ramDao = new ReadingAmmeterDao();
                    ramDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return ramDao;
            }
            //get { return ramDao; }
        }
        /// <summary>
        /// 定义抄水表工作单数据操作对象
        /// </summary>
        private static ReadingWatermeterDao rwmDao =  null;
        public static ReadingWatermeterDao ReadingWatermeterDao
        {
            get
            {
                if (rwmDao == null)
                {
                    rwmDao = new ReadingWatermeterDao();
                    rwmDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return rwmDao;
            }
           // get { return rwmDao; }
        }
         
        /// <summary>
        /// 定义维修工单数据操作对象
        /// </summary>
        private static RepairWorkDao rwDao = null;
        public static RepairWorkDao RepairWorkDao
        {
            get
            {
                if (rwDao == null)
                {
                    rwDao = new RepairWorkDao();
                    rwDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return rwDao;
            }
        }

        /// <summary>
        /// 定义申请报修数据操作对象
        /// </summary>
        private static ApplyToRepairDao atpDao = null;
        public static ApplyToRepairDao ApplyToRepairDao
        {
            get
            {
                if (atpDao == null)
                {
                    atpDao = new ApplyToRepairDao();
                    atpDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return atpDao;
            }
        }
        /// <summary>
        /// 定义维修工单数据操作对象
        /// </summary>
        private static UserDao userDao = null;
        public static UserDao UserDao
        {
            get
            {
                if (userDao == null)
                {
                    userDao = new UserDao();
                    userDao.ConnectionString = "data source=\"" + DB_FILE_PATH + "\"";
                }
                return userDao;
            }
        }

        public static void InitLocalDataBase()
        {
            if (!System.IO.File.Exists(DB_FILE_PATH))
            {
                SQLiteConnection.CreateFile(DB_FILE_PATH);

                InitTable();
            }
        }
        private static void InitTable()
        {
            //水表
            string sqlwatermeter = "CREATE TABLE WATERMETER(MPRId INTEGER PRIMARY KEY,WM_METER_ID VARCHAR2(20),"
                                + "READMONTH VARCHAR2(10),PRE_DEGREE INTEGER,READDEGREE INTEGER,DISTRICT_ID VARCHAR2(10),"
                                + "BUILDING_ID VARCHAR2(5),APARMENT_ID VARCHAR2(11),READINGTIME VARCHAR2(20),BARCODE VARCHAR2(50),PLAN_ID VARCHAR2(20))";
            //水表抄表计划记录表
            string sqlwatermeterpaln = "CREATE TABLE WATERMETERPLAN(mpId INTEGER PRIMARY KEY,planId VARCHAR2(20),"
                                + "month VARCHAR2(20),planStatus VARCHAR2(5),planName VARCHAR2(50),meterCount VARCHAR2(10))";
            //电表
            string sqlelectricitymeter = "CREATE TABLE ELECTRICITYMETER(MPRId INTEGER PRIMARY KEY,EM_METER_ID VARCHAR2(20) ,"
                                  + "READMONTH VARCHAR2(10),PRE_DEGREE INTEGER,READDEGREE INTEGER,DISTRICT_ID VARCHAR2(10),"
                                  + "BUILDING_ID VARCHAR2(5),APARMENT_ID VARCHAR2(11),READINGTIME VARCHAR2(20),BARCODE VARCHAR2(50),PLAN_ID VARCHAR2(20) )";
            //电表抄表计划记录表
            string sqlammeterpaln = "CREATE TABLE AMMETERPLAN(mpId INTEGER PRIMARY KEY,planId VARCHAR2(20),"
                                + "month VARCHAR2(20),planStatus VARCHAR2(5),planName VARCHAR2(50),meterCount VARCHAR2(10))";

            string strRepair = "CREATE TABLE repairWorkList(ASSIGNMENTID NUMBER(15),WONUM VARCHAR2(15),"
                        + "DESCRIPTION  VARCHAR2(100),LOCATION  VARCHAR2(12), LOCDESCRIPTION  VARCHAR2(100),ASSETNUM  VARCHAR2(12),"
                        + "ASSDESCRIPTION  VARCHAR2(100),STATUS  VARCHAR2(16),"
                        + "REMARK VARCHAR2(100), STATUSDATE  VARCHAR2(30),STARTTIME VARCHAR2(30),ENDTIME VARCHAR2(30))";

            string strCustomerWork = "CREATE TABLE CustomerWork(CWOrder_No VARCHAR2(15),Address VARCHAR2(50),"
                        + "User_Name  VARCHAR2(50),User_Phone  VARCHAR2(40), DESCRIPTION  VARCHAR2(100),Event_Level  VARCHAR2(30),"
                        + "STATUS  VARCHAR2(30),Comment  VARCHAR2(250), SR_NUMBER  VARCHAR2(30),UPDATEDT VARCHAR2(30))";

            //创建巡检工单主表
            string sqlInspection_l = "CREATE TABLE InspectionWork(ASSIGNMENTID NUMBER,WONUM VARCHAR2(10),"
                        + "DESCRIPTION  VARCHAR2(100),LOCATION  VARCHAR2(12), "
                        + "LOCDESCRIPTION  VARCHAR2(100),ASSETNUM  VARCHAR2(12),"
                        + "ASSDESCRIPTION  VARCHAR2(100),STATUS  VARCHAR2(16),REMARK VARCHAR2(100), STATUSDATE  VARCHAR2(30),STARTTIME VARCHAR2(30),ENDTIME VARCHAR2(30))";
            //创建巡检工单从表
            string sqlInspection_2 = "CREATE TABLE inspectionWorkDetail(ASSIGNMENTID NUMBER,TASKID NUMBER,"
                        + "DESCRIPTION VARCHAR2(100),STATUS VARCHAR2(16))";

            string strRepairAsswet = "CREATE TABLE RepairAsswet(WONUM VARCHAR2(20),BARCODE VARCHAR2(50),DETAIL VARCHAR2 (30),"
                        + "ACCOUNT  VARCHAR2(20),SPAREPARTID VAHCHAR2(20),CREATEDT VARCHAR2(20))";
            //申请报修表
            string strApplyToRepair = "CREATE TABLE APPLYTOREPAIR (Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,BARCODE VARCHAR2(50),ESTDUR VARCHAR2(50),"
                        + "RECORDE_ID VARCHAR2(20),APPLY_TIME VARCHAR2(20), REMARK VARCHAR2(100), REASON VARCHAR2(50),WORKSTATUS VARCHAR2(10), LOCATEDID VARCHAR2(50))"; 

            RepairWorkDao.executeNonQuery(strRepair);
            ReadingWatermeterDao.executeNonQuery(sqlwatermeter);
            ReadingWatermeterDao.executeNonQuery(sqlwatermeterpaln);

            ReadingAmmeterDao.executeNonQuery(sqlelectricitymeter);
            ReadingAmmeterDao.executeNonQuery(sqlammeterpaln);
            InspectionWorkDao.executeNonQuery(sqlInspection_l);
            InspectionWorkDao.executeNonQuery(sqlInspection_2);
            CustomerWorkDao.executeNonQuery(strCustomerWork);
            RepairWorkDao.executeNonQuery(strRepairAsswet);
            ApplyToRepairDao.executeNonQuery(strApplyToRepair);
            //登录记录表
            string sqlloginstate = " CREATE TABLE LOGINSTATE(ACCOUNT VARCHAR2(20),LOGINDT VARCHAR(20),STATE VARCHAR(10))";
            UserDao.executeNonQuery(sqlloginstate);
            //CustomerWorkDao.executeNonQuery(strCustomerWork);
            //InspectionWorkDao.executeNonQuery(sqlInspection_l);
            //InspectionWorkDao.executeNonQuery(sqlInspection_2);
        }
    }
}
